package com.ljfiction.book.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ljfiction.book.Dialog.NoBackgLodDialog;
import com.ljfiction.book.MyView.SuperButton;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.StringTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.request.BaseRequest;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class FindPasswordActivity extends BaseActivity {


    @BindView(R.id.back_find)
    ImageView backFind;
    @BindView(R.id.weibangdingtishi)
    LinearLayout weibangdingtishi;
    @BindView(R.id.setphonechange)
    EditText setphonechange;
    @BindView(R.id.yanzhnegmachange)
    EditText yanzhnegmachange;
    @BindView(R.id.fayanzhengma_find)
    SuperButton fayanzhengmaRetrieve;
    @BindView(R.id.next_find)
    SuperButton nextFind;
    @BindView(R.id.quhao_text_find)
    TextView quhaoTextFind;
    private CountDownTimer countDownTimer;
    private Intent intent;
    private NoBackgLodDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_password);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.back_find, R.id.fayanzhengma_find, R.id.next_find,R.id.quhao_find})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_find:
                goBack();
                break;
            case R.id.fayanzhengma_find:
                if ("".equals(setphonechange.getText().toString())) {
                    showToast("请输入手机号");
                } else if (StringTool.isEmpty(setphonechange.getText().toString())) {
                    showToast("请输入手机号");
                } else {
                    verifyPhone();
                }
                break;
            case R.id.next_find:
                if ("".equals(setphonechange.getText().toString())) {
                    showToast("请输入手机号");
                } else if (StringTool.isEmpty(setphonechange.getText().toString())) {
                    showToast("请输入手机号");
                } else if (!"".equals(yanzhnegmachange.getText().toString())) {
                    verification();
                } else {
                    showToast("请输入验证码");
                }
                break;
            case R.id.quhao_find:
                intent=new Intent(FindPasswordActivity.this,SelectquhaoActivity.class);
                startActivityForResult(intent,10);
                break;
        }
    }

    private void verification() {
        OkHttpUtils.post(HttpManager.IP + "/verification/person-checkedCode")

                .params("checkCode", yanzhnegmachange.getText().toString())
                .params("phoneId",StringTool.deletadd(quhaoTextFind.getText().toString())+ setphonechange.getText().toString())
                .execute(new StringCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        if (loadingDialog == null) {
                            loadingDialog = new NoBackgLodDialog(FindPasswordActivity.this, true);
                        }
                        loadingDialog.show();
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (loadingDialog != null) {
                            loadingDialog.dismis();
                        }
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if ("200".equals(jsonObject.getString("returnCode"))) {
                                intent = new Intent(FindPasswordActivity.this, FindPasswordnextActivity.class);
                                intent.putExtra("phoneId", quhaoTextFind.getText().toString().replace("+","")+"+"+setphonechange.getText().toString());
                                intent.putExtra("code", yanzhnegmachange.getText().toString());
                                startActivityForResult(intent, 2);
                            } else if ("500".equals(jsonObject.getString("returnCode"))) {
                                showToast(jsonObject.getString("msg"));
                            } else {
                                showToast(jsonObject.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

    }

    private void verifyPhone() {
        OkHttpUtils.get(HttpManager.IP + "/person-checkNickPhone/" + setphonechange.getText().toString())

                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if ("200".equals(jsonObject.getString("returnCode"))) {
                                if (weibangdingtishi.getVisibility() == View.INVISIBLE) {
                                    weibangdingtishi.setVisibility(View.VISIBLE);
                                }
                                showToast(jsonObject.getString("msg"));
                            } else if ("500".equals(jsonObject.getString("returnCode"))) {
                                if (weibangdingtishi.getVisibility() == View.VISIBLE) {
                                    weibangdingtishi.setVisibility(View.INVISIBLE);
                                }
                                sendMessage();

                            } else {
                                showToast("发送失败，请稍后再试");
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

    }

    private void sendMessage() {
        OkHttpUtils.post(HttpManager.IP + "/verification/sys-getShortMessage")

                .params("userMob",StringTool.deletadd(quhaoTextFind.getText().toString())+ setphonechange.getText().toString())
                .params("type",  StringTool.huoqugettype(quhaoTextFind.getText().toString(),"findPwd"))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if ("200".equals(jsonObject.getString("returnCode"))) {
                                showToast("发送成功");
                                countDownTimer = new CountDownTimer(60000, 1000) {
                                    @Override
                                    public void onTick(long millisUntilFinished) {
                                        fayanzhengmaRetrieve.setText("剩" + millisUntilFinished / 1000 + "秒");
                                        fayanzhengmaRetrieve.setClickable(false);
                                        fayanzhengmaRetrieve.setsSolidColor("#D8D8D8");
                                    }

                                    @Override
                                    public void onFinish() {
                                        fayanzhengmaRetrieve.setText("重新发送");
                                        fayanzhengmaRetrieve.setClickable(true);
                                        fayanzhengmaRetrieve.setsSolidColor("#f77583");
                                    }
                                };
                                countDownTimer.start();
                            } else {
                                showToast(jsonObject.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 2 && requestCode == 2) {
            goBack();
        }else if (requestCode==10&&resultCode==10){
            quhaoTextFind.setText(data.getStringExtra("countryNumber"));
        }
    }
}
